package com.kt.thread.demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by tangwy on 2016/3/26.
 */
public class ConditionTest2 {
    public void test() {
        Product product = new Product();
        Creator creator = new Creator(product);
        Consumer consumer = new Consumer(product);
        //两个线程生产
        new Thread(creator).start();
        new Thread(creator).start();
        //一个线程消费
        new Thread(consumer).start();
    }

    class Product {
        //产品的最大个数
        private static final int MAX = 10;
        private List<String> products = new ArrayList<String>(MAX);
        private final ReentrantLock lock = new ReentrantLock();
        private final Condition emptyCondition = lock.newCondition();
        private final Condition fullCondition = lock.newCondition();


        /**
         * 生产产品
         */

        public void set() {
            lock.lock();
            try {
                while (products.size()==MAX) {
                    emptyCondition.await();
                }
                String s = "产品" + UUID.randomUUID().toString();
                System.out.println("生产:" + s);
                products.add(s);
                fullCondition.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }

        public String get() {
            String s = null;
            lock.lock();
            try {
                while (products.isEmpty()) {
                    fullCondition.await();
                }
                int i = new Random().nextInt(products.size());
                s = products.get(i);
                products.remove(i);
                emptyCondition.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
            return s;
        }
    }

    /**
     * 生产者
     */
    class Creator implements Runnable {
        private Product product;

        public Creator(Product product) {
            this.product = product;
        }

        @Override
        public void run() {
            while (true) {
                try {
                    product.set();
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 消费者
     */
    class Consumer implements Runnable {
        private Product product;

        public Consumer(Product product) {
            this.product = product;
        }

        @Override
        public void run() {
            while (true) {
                try {
                    System.out.println("消费：" + product.get());
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


